export default {
  namespaced: true,
  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '' ,//用户成功登录后服务器下发的token
    userInfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 重定向信息对象
    redirectInfo: JSON.parse(uni.getStorageSync('redirectInfo') || 'null')
  }),

  mutations: {
    updateAddress(state, newAdd) {
      state.address = newAdd
      // 选择地址后，本地持久化存储地址
      this.commit('m_user/saveAddressToStorage')
    },
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    upateUserInfo(state, userinfo) {
      state.userInfo = userinfo
      this.commit('m_user/saveUserinfoToStorage')
    },
    saveUserinfoToStorage(state) {
      uni.setStorageSync('userinfo' , JSON.stringify(state.userInfo))
    },
    updateToken(state, token) {
      state.token = token
      this.commit('m_user/saveTokenToStorage')
    },
    saveTokenToStorage(state) {
      uni.setStorageSync('token', state.token)
    },
    updateRedirectInfo(state,redirectInfo ) {
      state.redirectInfo = redirectInfo
      // console.log(state.redirectInfo)
      this.commit('m_user/saveRedirectInfoToStorage')
    },
    saveRedirectInfoToStorage(state) {
      uni.setStorageSync('redirectInfo', JSON.stringify(state.redirectInfo))
    }
  },
  getters: {
    addStr(state) {
      if (!state.address.provinceName) return ''
      const { address } = state
      return address.provinceName + address.cityName + address.countyName + address.detailInfo
    }
  }
}
